package com.awesome.videodemo.util

/**
 * @ClassName: TimeFormatUtil
 * @Description: 日期格式化工具类
 * @Date: 2021-05-08 14:57
 */
object TimeFormatUtil {
    /**
     * @param duration 时长(毫秒)
     * @return 返回格式化后的时间, 如果时长为 0 则返回空字符串
     */
    fun formatDuration(duration: Long): String {
        var duration = duration
        val stringBuilder = StringBuilder()
        duration /= 1000
        // 拼接时
        val hour = duration / 60 / 60
        if (hour != 0L) {
            if (hour < 10) {
                stringBuilder.append("0").append(hour).append(":")
            } else {
                stringBuilder.append(hour).append(":")
            }
        }
        // 拼接分
        val minute = duration / 60 % 60
        if (minute < 10) {
            stringBuilder.append("0").append(minute).append(":")
        } else {
            stringBuilder.append(minute).append(":")
        }
        // 拼接秒
        val second = duration % 60
        if (second < 10) {
            stringBuilder.append("0").append(second)
        } else {
            stringBuilder.append(second)
        }
        return stringBuilder.toString()
    }
}